home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_6.lha / 6_6 / 6_6substr.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  765b  |  36 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / substr = str(3, 5)
  6. ifdef NOTDEF            // DELETE
  7. include <str.h>
  8. else                // DELETE
  9. include "6_6.h"        // DELETE
  10. endif                // DELETE
  11. ubstring string::operator()(int index, int count)
  12.  
  13.    char *s1 = str();
  14.    int s1len = strlen(s1);
  15.  
  16.    // convert left index, if necessary
  17.    if (index < 0)
  18. index += s1len;
  19.  
  20.    // left index past end of string
  21.    else if (index >= s1len)
  22. {
  23. substring sst(s1 + s1len, 0, this);
  24. return sst;
  25. }
  26.  
  27.    // convert count, if necessary
  28.    int numleft = s1len - index;
  29.    if (count > numleft || count < 0)
  30. count = numleft;
  31.  
  32.    // copy the substring
  33.    substring sst(s1 + index, count, this);
  34.    return sst;
  35.  
  36.